package operation;
import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @Author 12629
 * @Description：
 */
public class DelBook implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书....");
        Scanner scanner = new Scanner(System.in);

        //1、输入你要删除的图书名称：
        System.out.println("请输入要删除书籍的名称：");
        String name = scanner.nextLine();

        //2.查看当前书籍是否存在
        int index = -1;
        int i = 0;
        int currentSize = bookList.getUsedSize();
        for (; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //没有遇到break
        if(i >= currentSize) {
            System.out.println("没有你要删除的书....");
            return;
        }
        //遇到break  此时开始真正删除
        for (int j = index; j < currentSize-1; j++) {
            //bookList[j] = bookList[j+1]; error
            Book tmp = bookList.getBook(j+1);
            bookList.setBooks(j,tmp);
        }
        //修改usedSize的值

        bookList.setUsedSize(currentSize-1);

        bookList.setBooks(currentSize-1,null);

        System.out.println("删除图书成功了.....");
    }
}
